package io.github.zeleven.daysmatter;

import android.app.DatePickerDialog;
import android.app.Dialog;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.DialogFragment;
import android.view.View;
import android.widget.Button;
import android.widget.DatePicker;
import android.widget.EditText;
import android.widget.TextView;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * Created by user on 17-8-29.
 */

public class EventEditActivity extends BaseActivity implements View.OnClickListener {

    // The button to save event into database
    private Button saveEvent;

    // The EditText component to input the event content
    private EditText eventContentInput;

    // EventDao object used to manipulate the database
    private EventDao eventDao;

    // The TextView component to show current date time, and show the date time by user select
    public static TextView datetext;

    // get the event object to edit
    private Event eventToEdit;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.layoutId = R.layout.activity_addevent;
        super.onCreate(savedInstanceState);
//        setContentView(R.layout.activity_addevent);

        // Find button in add event layout and set it the click listener
        saveEvent = (Button) findViewById(R.id.save_event);
        saveEvent.setOnClickListener(this);

        // get data in intent, and get the event object by id
        Intent intent = getIntent();
        long eventId = intent.getLongExtra("eventId", -1);
        eventDao = ((App) getApplication()).getDaoSession().getEventDao();
        eventToEdit = eventDao.queryBuilder().where(EventDao.Properties.Id.eq(eventId)).
                list().get(0);

        // find event content edit text component in layout
        eventContentInput = (EditText) findViewById(R.id.event_content_input);
        eventContentInput.setText(eventToEdit.getEventContent());

        // find the datetime text view element in add event layout
        datetext = (TextView) findViewById(R.id.datetime);

        // Getting current date time and set for text view component
        Date date = eventToEdit.getTargetDate();
        String dateStr = new SimpleDateFormat("yyy-MM-dd").format(date);
        String weekdayStr = new SimpleDateFormat("EEEE").format(date);
        datetext.setText(dateStr + " " + weekdayStr);

        // Datetime text view component click listener that to show the date time picker
        datetext.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                DialogFragment datePickerFragment = new EventEditActivity.DatePickerFragment();
                datePickerFragment.show(getSupportFragmentManager(), "datePicker");
            }
        });
    }

    @Override
    public void onClick(View view) {
        eventToEdit.setEventContent(eventContentInput.getText().toString());
        String dateStr = datetext.getText().toString().split(" ")[0];
        try {
            eventToEdit.setTargetDate(new SimpleDateFormat("yyyy-MM-dd").parse(dateStr));
        } catch (ParseException e) {
            e.printStackTrace();
        }
        eventDao.update(eventToEdit);
        finish();
    }

    public static class DatePickerFragment extends DialogFragment implements
            DatePickerDialog.OnDateSetListener {

        @Override
        public Dialog onCreateDialog(Bundle savedInstanceState) {
            String[] dateStrArr = datetext.getText().toString().split(" ")[0].split("-");
            int year = Integer.parseInt(dateStrArr[0]);
            int month = Integer.parseInt(dateStrArr[1]) - 1;
            int day = Integer.parseInt(dateStrArr[2]);
            return new DatePickerDialog(getActivity(), this, year, month, day);
        }

        @Override
        public void onDateSet(DatePicker datePicker, int i, int i1, int i2) {
            String dateStr = "";
            String weekdayStr = "";
            try {
                dateStr = i + "-" + (i1 + 1) + "-" + i2;
                Date date = new SimpleDateFormat("yyyy-MM-dd").parse(dateStr);
                weekdayStr = new SimpleDateFormat("EEEE").format(date);

            } catch (ParseException e) {
                e.printStackTrace();
            }
            datetext.setText(dateStr + " " + weekdayStr);
        }
    }
}
